package com.hk.employeemanagementsystem.handler;

import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author by 何坤
 * @Classname Custom403AccessDeniedHandler
 * @Description TODO
 * @Date 2019/12/5 13:57
 */
@Component
public class Custom403AccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest httpServletRequest,
                       HttpServletResponse httpServletResponse,
                       AccessDeniedException e) throws IOException, ServletException {
        httpServletResponse.setContentType("application/json;charset=utf-8");
        httpServletResponse.setStatus(403);
        PrintWriter writer = httpServletResponse.getWriter();
        writer.write("{\"success\": false, \"message\": \"没有权限访问\"}");
        writer.flush();
        writer.close();
    }
}
